
 /**
 * OpenSessionInViewFilter.java.java.
 * User: BrianChen Date: 2012-7-29 上午11:15:54
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.aop.filter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-7-29 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-7-29 上午11:15:54
 * @version 1.0
 * @see
 * 
 * 缩窄OpenSessionInViewFilter的过滤范围,请求css、js、图片等静态内容时不创建/关闭Hibernate Session连接.
 * 在web.xml中可配置excludeSuffixs,excludePaths等参数,多个过滤文件后缀名以','分割.
 */
public class OpenSessionInViewFilter extends org.springframework.orm.hibernate3.support.OpenSessionInViewFilter{

	public static final String EXCLUDE_SUFFIXS_NAME = "excludeSuffixs";
	public static final String INCLUDE_SUFFIXS_NAME = "includeSuffixs";
	public static final String EXCLUDE_PATHS_NAME = "excludePaths";	
	
	private static final String[] DEFAULT_EXCLUDE_SUFFIXS ={ ".js", ".css", ".jpg", ".gif" };
	private static final String[] DEFAULT_INCLUDE_SUFFIXS ={ ".jsp" };
	private static final String[] DEFAULT_EXCLUDE_PATHS = { "/resources/", "/common/" };
	
	private String[] excludeSuffixs = DEFAULT_EXCLUDE_SUFFIXS;
	private String[] includeSuffixs = DEFAULT_INCLUDE_SUFFIXS;
	private String[] excludePaths = DEFAULT_EXCLUDE_PATHS;
	
	/**
	 * 重载过滤控制函数,忽略特定后缀名的请求.
	 */
	@Override
	protected boolean shouldNotFilter(final HttpServletRequest request) throws ServletException {
		String fullPath = request.getServletPath();
		String path = StringUtils.substringBefore(fullPath, "?");

		for (String pattern : excludePaths) {
			if(fullPath.contains(pattern))
				return true;
		}
		
		for (String suffix : excludeSuffixs) {
			if(path.endsWith(suffix))
				return true;
		}
		
		for (String suffix : includeSuffixs) {
			if(path.endsWith(suffix))
				return false;
		}

		return false;
	}

	/**
	 * 初始化includeSuffixs,excludeSuffixs参数
	 */
	@Override
	protected void initFilterBean() throws ServletException {

		String includeSuffixStr = getFilterConfig().getInitParameter(INCLUDE_SUFFIXS_NAME);

		if (StringUtils.isNotBlank(includeSuffixStr)) {
			includeSuffixs = includeSuffixStr.split(",");
			//为匹配字符串加上"."使匹配更精确，如.jpg
			for (int i = 0; i < includeSuffixs.length; i++) {
				includeSuffixs[i] = "." + includeSuffixs[i];
			}
		}

		String excludeSuffixStr = getFilterConfig().getInitParameter(EXCLUDE_SUFFIXS_NAME);

		if (StringUtils.isNotBlank(excludeSuffixStr)) {
			excludeSuffixs = excludeSuffixStr.split(",");
			for (int i = 0; i < excludeSuffixs.length; i++) {
				excludeSuffixs[i] = "." + excludeSuffixs[i];
			}
		}
		
		String excludePathStr = getFilterConfig().getInitParameter(EXCLUDE_PATHS_NAME);

		if (StringUtils.isNotBlank(excludePathStr)) {
			excludePaths = excludePathStr.split(",");
		}
	}
}

